// JavaScript Document
/**
 * [jquery滚动条插件]
 */
(function($) {
    $.fn.extend({
        'scrollBar':function(options) {
            var defaults = {
                listW:200,
                //列表宽
                listH:300,
                //列表高
                speed:10,
                //滚动速度
                autoHide:0,
                //设置滚动条是否自动隐藏 1:自动隐藏 0：不自动隐藏， 默认不隐藏
                CSS:1
            };
            var config = $.extend(defaults, options);
            return this.each(function() {
                var nowthis = $(this);
                var documentEvent, documentEvent2;
                if (nowthis.outerHeight(true) < config.listH) {
                    nowthis.siblings("#scroll_bar").remove();
                    nowthis.unwrap().removeAttr("style");
                } else {
                    _wrapHtml(nowthis, config.listH, config.listW);
                    if (config.CSS !== 1) {
                        nowthis.siblings("#scroll_bar").css(config.CSS);
                    } else {
                        nowthis.siblings("#scroll_bar").css({
                            margin:0,
                            padding:0,
                            background:"#f9f9f9",
                            border:"1px solid #ccc",
                            "border-radius":"5px"
                        });
                    }
                    var overH = _getOverHeght(nowthis, config.listH);
                    _removeEvent(nowthis,documentEvent, documentEvent2);
                    _dragBar(nowthis, overH);
                    _mouseWheelBar(nowthis, overH, config.listH, config.speed);
                    if (config.autoHide) {
                        _isBarHide();
                    }
                }
            });
        }
    });
    /**
     * [_wrapHtml description]
     * @param  {[obj]} nowthis [当前标签]
     * @param  {[int]} listH   [列表高]
     * @param  {[int]} listW   [列表宽]
     */
    function _wrapHtml(nowthis, listH, listW) {
        if (nowthis.parent("#scroll_frame").size()) return false;
        nowthis.wrap('<div id="scroll_frame"></div>');
        nowthis.parent("#scroll_frame").prepend('<div id="scroll_bar"><div>');
        nowthis.parent("#scroll_frame").css({
            overflow:"hidden",
            height:listH,
            width:listW,
            position:"relative"
        });
        nowthis.siblings("#scroll_bar").css({
            position:"absolute",
            right:0,
            top:0,
            "z-index":2,
            width:"7px",
            background:"#f9f9f9"
        });
        nowthis.css({
            position:"absolute",
            "z-index":1
        }).outerWidth(listW);
    }
    /**
     * [_getOverHeght 设置可移动高度]
     * @param  {[obj]} nowthis [当前标签]
     * @param  {[int]} listH   [列表高]
     * @return {[int]}         [可移动的高度]
     */
    function _getOverHeght(nowthis, listH) {
        //超过的
        var overH = nowthis.outerHeight(true) - listH;
        var DragH = 0;
        //设置滚动条高度
        if (overH < listH - 20) {
            nowthis.siblings("#scroll_bar").outerHeight(listH - overH);
            DragH = 1;
        } else {
            nowthis.siblings("#scroll_bar").outerHeight(listH / 5);
            //可移动高度
            DragH = overH / (listH - listH / 5);
        }
        var buttomH  = listH - nowthis.siblings("#scroll_bar").position().top - nowthis.siblings("#scroll_bar").outerHeight(true);
        var isbottom = nowthis.outerHeight(true) + nowthis.position().top;
        if (isbottom < listH) {
            nowthis.css("top", -overH);
        }
        if (buttomH < 0) {
            nowthis.siblings("#scroll_bar").css("top", overH);
        }
        return DragH;
    }
    /**
     * [_dragBar 滚动条拖拽函数]
     * @param  {[obj]} nowthis        [当前的标签]
     * @param  {[int]} overH          [超过列表的高度]
     * @param  {[function]} documentEvent  [document事件1]
     * @param  {[function]} documentEvent2 [document事件2]
     */
    function _dragBar(nowthis, overH, documentEvent, documentEvent2) {
        var x, y, nowLeft, nowTop, marginY, marginX;
        var move = false;
        var _this = nowthis.siblings("#scroll_bar");
        _this.bind("mousedown", function(e) {
            move = true;
            _this.css({
                opacity:1
            });
            y = e.pageY;
            marginY = (_this.outerHeight(true) - _this.outerHeight()) / 2;
            //当前位置
            nowTop = _this.position().top;
            $(document).bind("mousemove", documentEvent).bind("mouseup", documentEvent2);
            return false;
        }).bind("mouseup", function() {
            move = false;
        }).bind("mouseenter", function() {
            _this.css({
                cursor:"move"
            });
        });
        documentEvent = function(e) {
            if (!move) return false;
            //最终位移=移动位置+当前位置
            var limitY = e.pageY - y + nowTop;
            var maxY = nowthis.parent("#scroll_frame").innerHeight() - _this.outerHeight() - marginY;
            limitY = limitY < -marginY ? -marginY :limitY;
            limitY = limitY < maxY ? limitY :maxY;
            //限制窗口
            _this.css({
                opacity:1,
                top:limitY
            });
            nowthis.css({
                top:-overH * limitY
            });
            return false;
        };
        documentEvent2 = function(event) {
            move = false;
        };
        
    }
    /**
     * [_mouseWheelBar 滚轮事件]
     * @param  {[obj]} nowthis        [当前的标签]
     * @param  {[int]} overH          [超过列表的高度]
     * @param  {[int]} listH    [列表高]
     * @param  {[int]} speed   [滚动速度]
     * @return {[bool]}         [false]
     */
    function _mouseWheelBar(nowthis, overH, listH, speed) {
        nowthis.parent("#scroll_frame").bind("mousewheel", function(event, delta) {
            var Y = parseInt(nowthis.siblings("#scroll_bar").position().top);
            var H = listH - nowthis.siblings("#scroll_bar").outerHeight(true);
            Y = Y + speed * -delta;
            //滚动条范围
            Y = Y <= 0 ? 0 :Y;
            Y = Y >= H ? H :Y;
            nowthis.siblings("#scroll_bar").css({
                opacity:1,
                top:Y
            });
            nowthis.css({
                top:-overH * Y
            });
            return false;
        });
    }
    /**
     * [_removeEvent 移除事件]
     * @param  {[function]} documentEvent  [document事件1]
     * @param  {[function]} documentEvent2 [document事件2]
     */
    function _removeEvent(nowthis,documentEvent, documentEvent2) {
        nowthis.siblings("#scroll_bar").unbind();
        nowthis.parent("#scroll_frame").unbind();
        $(document).unbind("mousemove", documentEvent);
        $(document).unbind("mouseup", documentEvent2);
    }
    //
    /**
     * [_isBarHide 设置滚动条是否自动隐藏]
     */
    function _isBarHide() {
        nowthis.parent("#scroll_frame").bind("mouseenter", function() {
            nowthis.siblings("#scroll_bar").animate({
                opacity:1
            }, 200);
        }).bind("mouseleave", function() {
            setTimeout(function() {
                nowthis.siblings("#scroll_bar").animate({
                    opacity:0
                }, 200);
            }, 4e3);
        });
    }
})(jQuery);